package cn.lger.service;

import cn.lger.dao.CommentDao;
import cn.lger.pojo.Comment;
import cn.lger.pojo.Contact;
import cn.lger.pojo.Page;
import cn.lger.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Code that Changed the World
 *
 * @author Pro
 * @date 2018-06-16.
 */
@Service
public class CommentService {

    @Resource
    private CommentDao commentDao;

    @Transactional(rollbackFor = Exception.class)
    public int insertOne(Comment comment){
        return commentDao.insertOne(comment);
    }

    public Page<Comment> findOnePageByArticleId(int id, Integer currentPage, Integer size) {
        Page<Comment> page = PageUtil.generator(currentPage, size);
        List<Comment> list = commentDao.findOnePageByArticleId(id, page.getPage(), page.getSize());
        page.setContent(list);
        page.setCount(commentDao.getTotalByArticleId(id));
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    public int deleteOneById(int id) {
        return commentDao.deleteOneById(id);
    }

    public Page<Comment> findOnePage(Integer currentPage, Integer size) {
        Page<Comment> page = PageUtil.generator(currentPage, size);
        List<Comment> list = commentDao.findOnePage(page.getPage(), page.getSize());
        page.setContent(list);
        page.setCount(commentDao.getTotal());
        return page;
    }
}
